package gnu.io;

import java.io.*;
import java.awt.*;
import java.awt.event.*;

class Configure extends Frame
{
    Checkbox[] cb;
    Panel p1;
    static final int PORT_SERIAL = 1;
    static final int PORT_PARALLEL = 2;
    int PortType;
    String EnumMessage;
    
    private void saveSpecifiedPorts() {
        final String property = System.getProperty("java.home");
        final String property2 = System.getProperty("path.separator", ":");
        final String property3 = System.getProperty("file.separator", "/");
        final String property4 = System.getProperty("line.separator");
        String s;
        if (this.PortType == 1) {
            s = property + property3 + "lib" + property3 + "gnu.io.rxtx.SerialPorts";
        }
        else {
            if (this.PortType != 2) {
                System.out.println("Bad Port Type!");
                return;
            }
            s = property + "gnu.io.rxtx.ParallelPorts";
        }
        System.out.println(s);
        try {
            final FileOutputStream fileOutputStream = new FileOutputStream(s);
            for (int i = 0; i < 128; ++i) {
                if (this.cb[i].getState()) {
                    fileOutputStream.write((this.cb[i].getLabel() + property2).getBytes());
                }
            }
            fileOutputStream.write(property4.getBytes());
            fileOutputStream.close();
        }
        catch (IOException ex) {
            System.out.println("IOException!");
        }
    }
    
    void addCheckBoxes(final String s) {
        for (int i = 0; i < 128; ++i) {
            if (this.cb[i] != null) {
                this.p1.remove(this.cb[i]);
            }
        }
        for (int j = 1; j < 129; ++j) {
            this.cb[j - 1] = new Checkbox(s + j);
            this.p1.add("NORTH", this.cb[j - 1]);
        }
    }
    
    public Configure() {
        this.PortType = 1;
        this.EnumMessage = "gnu.io.rxtx.properties has not been detected.\n\nThere is no consistant means of detecting ports on this operating System.  It is necessary to indicate which ports are valid on this system before proper port enumeration can happen.  Please check the ports that are valid on this system and select Save";
        final int n = 640;
        final int n2 = 480;
        this.cb = new Checkbox[128];
        final Frame frame = new Frame("Configure gnu.io.rxtx.properties");
        String s;
        if (System.getProperty("file.separator", "/").compareTo("/") != 0) {
            s = "COM";
        }
        else {
            s = "/dev/";
        }
        frame.setBounds(100, 50, n, n2);
        frame.setLayout(new BorderLayout());
        (this.p1 = new Panel()).setLayout(new GridLayout(16, 4));
        final ActionListener actionListener = new ActionListener() {
            public void actionPerformed(final ActionEvent actionEvent) {
                if (actionEvent.getActionCommand().equals("Save")) {
                    Configure.this.saveSpecifiedPorts();
                }
            }
        };
        this.addCheckBoxes(s);
        final TextArea textArea = new TextArea(this.EnumMessage, 5, 50, 3);
        textArea.setSize(50, n);
        textArea.setEditable(false);
        final Panel panel = new Panel();
        panel.add(new Label("Port Name:"));
        final TextField textField = new TextField(s, 8);
        textField.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent actionEvent) {
                Configure.this.addCheckBoxes(actionEvent.getActionCommand());
                frame.setVisible(true);
            }
        });
        panel.add(textField);
        panel.add(new Checkbox("Keep Ports"));
        final Button[] array = new Button[6];
        for (int n3 = 0, i = 4; i < 129; i *= 2, ++n3) {
            (array[n3] = new Button("1-" + i)).addActionListener(new ActionListener() {
                public void actionPerformed(final ActionEvent actionEvent) {
                    for (int int1 = Integer.parseInt(actionEvent.getActionCommand().substring(2)), i = 0; i < int1; ++i) {
                        Configure.this.cb[i].setState(!Configure.this.cb[i].getState());
                        frame.setVisible(true);
                    }
                }
            });
            panel.add(array[n3]);
        }
        final Button button = new Button("More");
        final Button button2 = new Button("Save");
        button.addActionListener(actionListener);
        button2.addActionListener(actionListener);
        panel.add(button);
        panel.add(button2);
        frame.add("South", panel);
        frame.add("Center", this.p1);
        frame.add("North", textArea);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(final WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }
    
    public static void main(final String[] array) {
        new Configure();
    }
}
